<template>
  <div style="width: 100%;height: 100%;display: flex">
    <div class="left_box">
      <div class="code_box">
        <ace-editor  style="width: 100%;height: 100%" :html-text-value="htmlStr" :ts-text-value="tsStr"></ace-editor>
      </div>
    </div>
    <div id="load4490_box">
      <div class="popup_box">
        <div class="inner_content_common">
          <el-button type="primary" @click="changeTdt(false)" size="small">正常天地图</el-button>
          <el-button type="primary" @click="changeTdt(true)" size="small">暗黑天地图</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>

import {initMap} from "@/widgets/modelExample_cesium/common/initMap";
import {loadTdtLayer} from "@/widgets/modelExample_cesium/common/loadLayers";
import {onMounted, ref} from "vue";
import AceEditor from "@/components/aceEditor.vue";

const htmlStr=ref(`
<template>
  <div style="width: 100%;height: 100%;display: flex">
    <div id="load4490_box">
      <div class="popup_box">
        <div class="inner_content_common">
          <el-button type="primary" @click="changeTdt(false)" size="small">正常天地图</el-button>
          <el-button type="primary" @click="changeTdt(true)" size="small">暗黑天地图</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>

import {initMap} from "@/widgets/modelExample_cesium/common/initMap";
import {loadTdtLayer} from "@/widgets/modelExample_cesium/common/loadLayers";
import {onMounted, ref} from "vue";

const cViewer = ref();

const changeTdt = (isHei: boolean) => {
  loadTdtLayer(cViewer.value, 'w', isHei);
}
const toolbar = ref({})
onMounted(() => {
  cViewer.value = initMap("load4490_box")
})
<\/script>

<style lang="scss" scoped>

#load4490_box {
  width: 60%;
  height: 100%;
}

.popup_box {
  position: fixed;
  left: calc(40% + 100px);
  width: 200px;
  top: 100px;
  height: 60px;
  z-index: 10;
}

.inner_content_common {
  margin-top: 10px;
  display: flex;
  justify-content: center;
  width: 50%;
}
</style>
`);
const tsStr=ref(`
import {
    ArcGisMapServerImageryProvider,
    WebMapTileServiceImageryProvider,
} from 'cesium'
import Cesium from "cesium";
/**
 * 加载天地图
 * @param viewer
 * @param type
 */
const loadTdtLayer=(viewer:any,type:string,isAnHei:boolean=false)=>{
    viewer.imageryLayers.removeAll();
    const tdtKeys=[
    '18992e1edf6d198f398c38c5eee5aad9',
    '452fa1da60ebc9ea7c09702678f1fb96',
    'f50150db8ad49aec65c54db85d313a85',
    '576ab0b068f9559271b1c8cc65653384',
    'c57ca7cce19c08cf8d982bfddf581ccf',
    'f424a5e7adbe5c596ff4013c9e437ce9']
    let  num=Math.floor(Math.random()*(5-1)+1);
    let currentKeys=tdtKeys[num];
    //在线天地图影像服务地址(墨卡托投影)
    var TDT_IMG_W = "http://{s}.tianditu.gov.cn/img_w/wmts?service=wmts&request=GetTile&version=1.0.0" +
        "&LAYER=img&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}" +
        "&style=default&format=tiles&tk=" + currentKeys;
    //在线天地图矢量地图服务(墨卡托投影)
    var TDT_VEC_W = "http://{s}.tianditu.gov.cn/vec_w/wmts?service=wmts&request=GetTile&version=1.0.0" +
        "&LAYER=vec&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}" +
        "&style=default&format=tiles&tk=" + currentKeys;
    //在线天地图影像中文标记服务(墨卡托投影)
    var TDT_CIA_W = "http://{s}.tianditu.gov.cn/cia_w/wmts?service=wmts&request=GetTile&version=1.0.0" +
        "&LAYER=cia&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}" +
        "&style=default.jpg&tk=" + currentKeys
    //在线天地图矢量中文标记服务(墨卡托投影)
    var TDT_CVA_W = "http://{s}.tianditu.gov.cn/cva_w/wmts?service=wmts&request=GetTile&version=1.0.0" +
        "&LAYER=cva&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}" +
        "&style=default.jpg&tk=" +currentKeys;
    //在线天地图地形地图服务(墨卡托投影)
    var TDT_TER_W = "http://{s}.tianditu.gov.cn/ter_w/wmts?service=wmts&request=GetTile&version=1.0.0" +
        "&LAYER=ter&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}" +
        "&style=default.jpg&tk=" + currentKeys;
    //在线天地图地形中文标记服务(墨卡托投影)
    var TDT_CTA_W = "http://{s}.tianditu.gov.cn/cta_w/wmts?service=wmts&request=GetTile&version=1.0.0" +
        "&LAYER=cta&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}" +
        "&style=default.jpg&tk=" + currentKeys;
    let mapUrl = TDT_IMG_W;
    let labelUrl = TDT_CIA_W;
    if (type === 'c'){
        // 影像图
        mapUrl = TDT_IMG_W;
        labelUrl = TDT_CIA_W;
    } else if (type === 'w'){
        // 矢量图
        mapUrl = TDT_VEC_W;
        labelUrl = TDT_CVA_W;
    } else if (type ==='d'){
        // 地形图
        mapUrl = TDT_TER_W;
        labelUrl = TDT_CTA_W;
    }
    let Img = new  WebMapTileServiceImageryProvider({
        //调用影像中文服务
        url: mapUrl,
        //WMTS请求的层名称
        layer: "ter_w",
        //WMTS请求的样式名称
        style: "default",
        //MIME类型，用于从服务器检索图像
        format: "image/jpeg",
        //用于WMTS请求的TileMatrixSet的标识符
        tileMatrixSetID: "GoogleMapsCompatible",
        //天地图8个服务器
        subdomains: ["t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"],
        minimumLevel: 0,
        maximumLevel: 18,
    })
    let images= viewer.imageryLayers.addImageryProvider(Img)
    if(isAnHei){
        //图层的色调
        images.hue=3;
        //图层对比度
        images.contrast=-1.2
    }
    // images.brightness=0.4
    //调用影像中文注记服务
    let cia = new  WebMapTileServiceImageryProvider({
        url: labelUrl,
        layer: "cta_w",
        style: "default",
        format: "tiles",
        tileMatrixSetID: "GoogleMapsCompatible",
        subdomains: ["t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"],
        minimumLevel: 0,
        maximumLevel: 18,
    })
    viewer.imageryLayers.addImageryProvider(cia)
}
export {
    loadTdtLayer
}
`);
const cViewer = ref();

const changeTdt = (isHei: boolean) => {
  loadTdtLayer(cViewer.value, 'w', isHei);
}
const toolbar = ref({})
onMounted(() => {
  cViewer.value = initMap("load4490_box")
})
</script>

<style lang="scss" scoped>
.left_box {
  width: 40%;
  height: 100%;
  display: flex;
  flex-direction: row-reverse;

  .code_box {
    margin: 10px;
    width: calc(100% - 220px);
    height: calc(100% - 20px);
    //border:1px solid #00ff00;
    background-color: #ffffff;
    color: #000;
    overflow-x: hidden;
    overflow-y: auto;
  }
}

#load4490_box {
  width: 60%;
  height: 100%;
}

.popup_box {
  position: fixed;
  left: calc(40% + 100px);
  width: 200px;
  top: 100px;
  height: 60px;
  z-index: 10;
}

.inner_content_common {
  margin-top: 10px;
  display: flex;
  justify-content: center;
  width: 50%;
}
</style>
